package com.chixing.day18_thread;

import org.apache.poi.ss.formula.functions.T;

public class PubliicResourse {
    private int num = 7;

    //生产面包
    public synchronized void produce(){
        if (num==20){
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }else {
            num++;
            System.out.println("生产者生产了，当前面包："+num);

            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            notify();
        }
    }
    //消费面包
    public synchronized void consume(){
        if (num<=3){
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }else {
            int i = (int) Math.ceil(Math.random()*3);
            num-=i;
            System.out.println("消费者消费了"+i+"个，当前面包："+num);
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            notify();
        }
    }
}
